﻿using System;
using System.Web.Services;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.WarrantyTickets
{
    public partial class ProductAtSupplier : System.Web.UI.Page
    {
        #region Properties

        private static WarrantyProductBiz _warrantyProductBiz;

        private static WarrantyProductBiz WarrantyProductManager
        {
            get { return _warrantyProductBiz ?? (_warrantyProductBiz = new WarrantyProductBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.AdminBaoHanh))
            {
                Response.Redirect("~/NoPermission.aspx");
            }
        }
        
        [WebMethod(EnableSession = true)]
        public static object ProductsAtSupplier(int jtStartIndex, int jtPageSize, string jtSorting)
        {
            var ticketCount = WarrantyProductManager.GetWarrantyProductsAtSupplierCount();
            var products = WarrantyProductManager.GetWarrantyProductsAtSupplier(jtStartIndex, jtPageSize, jtSorting);
            foreach (var product in products)
            {
                // product.CreatedTimeString = string.Format("{0:HH:mm}", product.CreatedDate);
                switch (product.ReceivedFrom)
                {
                    case 1:
                        product.ReceivedFromName = Resources.GlobalStrings.Customers;
                        break;
                    case 2:
                        product.ReceivedFromName = Resources.GlobalStrings.Employees;
                        product.DealerEmployeeName = product.FromEmployeeName;
                        break;
                    case 3:
                        product.ReceivedFromName = Resources.GlobalStrings.Dealers;
                        product.DealerEmployeeName = product.FromDealerName;
                        break;
                }
            }

            return new { Result = "OK", Records = products, TotalRecordCount = ticketCount };
        }
    }
}